package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 购物车接口设置
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //查找购物车数据，查看是否存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //获取用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
         List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
         if(list!=null && list.size()>0){
             //存在就给数量加1
             ShoppingCart cart = list.get(0);
             cart.setNumber(cart.getNumber()+1);
              //更新数据
             shoppingCartMapper.updateShopping(cart);
         }else {
              //先判断是菜品还是套餐
             Long dishId = shoppingCartDTO.getDishId();
             if(dishId!=null){
                 //查找对应商品
                 Dish dish = dishMapper.queryDish(dishId);
                 shoppingCart.setName(dish.getName());
                 shoppingCart.setImage(dish.getImage());
                 shoppingCart.setAmount(dish.getPrice());
             }else {
                 Long setmealId = shoppingCartDTO.getSetmealId();
                 Setmeal setmeal = setmealMapper.queryBySetmeal(setmealId);
                 shoppingCart.setAmount(setmeal.getPrice());
                 shoppingCart.setImage(setmeal.getImage());
                 shoppingCart.setName(setmeal.getName());
             }
             shoppingCart.setNumber(1);
             shoppingCart.setCreateTime(LocalDateTime.now());
             //不存在就添加购物车数据
             shoppingCartMapper.insertShopping(shoppingCart);
         }


    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> listResult() {
        ShoppingCart cart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        List<ShoppingCart> list = shoppingCartMapper.list(cart);
        return list;
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppinCart() {
        shoppingCartMapper.cleanShoppinCart(BaseContext.getCurrentId());
    }

    /**
     * 减少商品
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //查找购物车数据，查看是否存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //获取用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        ShoppingCart cart = list.get(0);
        if(cart.getNumber()>1){
            //存在就给数量加1
            cart.setNumber(cart.getNumber()-1);
            shoppingCartMapper.updateShopping(cart);
        }else {
            Long dishId = shoppingCartDTO.getDishId();
            if(dishId!=null){
                shoppingCartMapper.deleteByDishId(dishId);
            }else {
                shoppingCartMapper.deleteBySetmealId(shoppingCartDTO.getSetmealId());
            }
        }
    }
}
